'''

    Dock widget classes and tools

'''

from PyQt4 import QtGui, QtCore

import SHIK.ui.view.factory as viewFactory
import SHIK.ui.resources

class DockViewTitleBar( QtGui.QFrame ):
    def __init__( self, dock ):
        QtGui.QFrame.__init__( self )
        self.setObjectName("aDockTitle")
        self.setFrameShape(QtGui.QFrame.Panel|QtGui.QFrame.Raised)
        
        lo = QtGui.QBoxLayout( QtGui.QBoxLayout.LeftToRight, self )
        lo.setMargin(0)
        
        self.select = QtGui.QToolButton(self)
        #select.setIcon( rsc.GetIcon( 'ink' ) )
        self.select.setMenu(viewFactory.create_menu(self, self.SetViewType))
        self.select.setPopupMode(QtGui.QToolButton.InstantPopup)
        
        close = QtGui.QToolButton(self)
        closeAction = QtGui.QAction( QtGui.QIcon(':img/clear'), 'Close', close )
        #closeAction = QtGui.QAction( 'Close', close )
        closeAction.setToolTip("Close this panel")
        closeAction.setStatusTip("Close this panel")
        self.connect(
            closeAction, QtCore.SIGNAL( "triggered(QAction*)"), self.OnTriggered
        )
        close.setDefaultAction(closeAction)
        self.connect(
            close, QtCore.SIGNAL( "triggered(QAction*)"), self.OnTriggered
        )
        
        lo.addWidget( self.select )
        lo.addStretch( 100 )
        lo.addWidget( close )
        
        self.setLayout( lo )
    
    def resize( self, *args ):
        QtGui.QWidget.resize( self, *args )
        self.setMinimumSize( *args )
    
    def minimumSizeHint( self ):
        if self.getWidget():
            s = self.getWidget().minimumSizeHint()
            return QtCore.QSize( s.width()+5, s.height()+32 )
        return QtCore.QSize( 200, 200 )
    
    def getDock(self):
        return self.parentWidget()
    
    def getWidget(self):
        d = self.getDock()
        if d is None:
            return None
        return d.widget()
    
    def OnTriggered( self, action ):
        dock = self.getDock()
        if action.text() == 'Close':
            dock.parentWidget().removeDockWidget( dock )
            return
    
    def SetViewType( self, viewType ):
        view = viewFactory.create(viewType, None)
        #view.setStyleSheet( "* { background-color: #FF8800 } " )
        view.setMinimumSize( 50, 50 )
        self.getDock().setWidget(view)
        self.getDock().setWindowTitle(viewType)
        self.select.setText(viewType)
        return view
        
    def OnClose(self):
        print 'close'


class MainWindow(QtGui.QMainWindow):
    def __init__(self, *args):
        QtGui.QMainWindow.__init__(self, *args)
        self.resize( 800, 600 )

        # config
        self.setDockNestingEnabled( True )
        self.setDockOptions(
            QtGui.QMainWindow.VerticalTabs
        )
        
        # status bar
        status = self.statusBar()
        status.setSizeGripEnabled(False)
        statuslabel = QtGui.QLabel()
        statuslabel.setFrameStyle(QtGui.QFrame.StyledPanel|QtGui.QFrame.Sunken)
        status.addPermanentWidget(statuslabel)
        
        # Menu bar (Mac Way)
        menuBar = QtGui.QMenuBar(None)
        self.setMenuBar(menuBar)
        sessionMenu = menuBar.addMenu("Session")
        #sessionMenu.addAction( "&Restore", self.RestoreSession )
        #sessionMenu.addAction( "&Save", self.SaveSession )
        sessionMenu.addAction( "&Quit", self.close )
        
        viewMenu = menuBar.addMenu("Views")
        viewMenu.addAction( "Add View", self._AddView )
        
        # toolbar
        mainTB = self.addToolBar( "App" )
        mainTB.setObjectName( "AppToolBar" )
        mainTB.addAction( "Add View", self._AddView )

    def _AddDock( self, title, w, area=QtCore.Qt.LeftDockWidgetArea ):
        dock = QtGui.QDockWidget( title )
        
        dock.setFeatures(
            QtGui.QDockWidget.AllDockWidgetFeatures
            #| QtGui.QDockWidget.DockWidgetVerticalTitleBar
        )
        dock.setAllowedAreas(
            QtCore.Qt.RightDockWidgetArea
            | QtCore.Qt.LeftDockWidgetArea
            | QtCore.Qt.TopDockWidgetArea
            | QtCore.Qt.BottomDockWidgetArea
        )
        
        t = DockViewTitleBar(dock)
        t.select.setText(title)
        dock.setTitleBarWidget(t)
        self.addDockWidget(area, dock)
        dock.setWidget(w)
        return dock

    def _AddView(self, type='View', area=QtCore.Qt.LeftDockWidgetArea):
        w = viewFactory.create(type, None)
        if not w:
            w = QtGui.QWidget()
            w.setStyleSheet("* { background-color: #FF8800 } ")
            w.setMinimumSize(100, 100)
        dock = self._AddDock(type, w, area)
        return dock
        
    
